//
//  LauNetworkManager+Extension.swift
//  SwiftProject
//
//  Created by Lausen on 2017/7/25.
//  Copyright © 2017年 id. All rights reserved.
//

import Foundation

extension LauNetworkManager{
    
    /// 封装微博数据的请求
    ///   - since_id: 返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
    ///   - max_id: 返回ID小于或等于max_id的微博，默认为0
    /// - Parameter completion: 完成的回调
    func statusList(since_id: Int64 = 0,max_id: Int64 = 0,completion:@escaping (_ list:[[String: Any]]?,_ isSuccess: Bool) -> ()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        ///Users/lausen/Desktop/SwiftProject7.4/SwiftProject/SwiftProject/Classes/Tool(工具类)/LauNetWork/LauNetworkManager+Extension.swift:22:51: Nil is not compatible with expected argument type '[String : Any]'
        let params = [
            "since_id": since_id,
            "max_id": max_id > 0 ? max_id - 1 : 0
        ]
        
        
        tokenRequest(URLString: urlString, parameters: params, completion: { (json, isSuccess) in
        
            if isSuccess {
                
                let result = json as? [String: Any]
                let statusJson = result?["statuses"] as? [[String: Any]]
                completion(statusJson, isSuccess)
            }
        })
    }
    
    
    /// 返回未读的微博的条数
    ///
    /// - Parameter completion: 请求成功的回调
    func unreadCount(completion: @escaping(_ count: Int) -> ()) {
        
        let urlString = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
        guard let uid = account.uid else {
            
            return
        }
        
        let params = ["uid" : uid]
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            let dict = json as? [String: Any]
            
            let count = dict?["status"] as? Int ?? 0
            
            completion(count)
        }
    }
    
    //MARK: -- 用户信息的请求方法
    func loadUserInfo(completion: @escaping (_ dict: [String: Any]) -> ()) {
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        guard let uid = account.uid else {
            
            return
        }
        
        var params = [String: Any]()
        
        params["uid"] = uid
        
        tokenRequest(URLString: urlString, parameters: params) { (json, isSuccess) in
            
            completion((json as? [String: Any] ?? [:]))
        }
    }
    
    //MARK: -- 换取token的网络请求方法
    func loadAccessToken(code: String,completion: @escaping(_ isSuccess: Bool)->()) {
        
        //请求连接
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        //请求参数
        var params = [String: Any]()
        
        params["client_id"] = LauAppKey
        params["client_secret"] = LauAppSecret
        params["grant_type"] = "authorization_code"
        params["code"] = code
        params["redirect_uri"] = LauRedirectURI
        
        request(method: .POST, URLString: urlString, parameters: params) { (json, isSuccess) in
            
            //1:将请求回来的json设置成账户模型
            self.account.yy_modelSet(withJSON: json ?? [:])
            
            print(self.account)
            
            //请求成功之后立即去请求用户的信息
            self.loadUserInfo(completion: { (dict) in
                
                //重新设置了模型
                self.account.yy_modelSet(with: dict)
                
                print(self.account)
                
                //2:调用保存账户的方法
                self.account.saveAccount()
                
                //3:请求完成的回调
                completion(isSuccess)
            })
        }
    }
}
